<template>
  <van-form :loading="loading">
    <VanFieldCheckbox
      name="Users"
      label="收件人"
      v-model="formFields.Users"
      :columns="useroption"
      :option="option"
      :rules="[{ required: true, message: '必填' }]"
    />
    <van-field
      v-model="formFields.Title"
      name="Title"
      label="主题"
      :rules="[{ required: true, message: '必填' }]"
    />
    <van-field name="Appendix" label="附件">
      <template #input>
        <c-upload-file
          v-model="formFields.Appendix"
          :maxCount="1"
        ></c-upload-file>
      </template>
    </van-field>
    <in-editor v-model="formFields.Text"></in-editor>

    <div style="margin:16px; text-align:center;">
      <van-button type="info" v-if="!disabled" @click="handleSubmit(true)"
        >存草稿</van-button
      >
      <van-button
        style="margin-left:16px;"
        v-if="!disabled"
        type="info"
        @click="handleSubmit(false)"
        >发送</van-button
      >
      <van-button
        style="margin-left:16px;"
        type="default"
        @click="handleCancel"
        >{{ closeName }}</van-button
      >
    </div>
  </van-form>
</template>

<script>
import {
  D_UserMail_GetTheData,
  D_UserMail_SaveData
} from "@/api/D_Manage/D_UserMail";
import InEditor from "@/components/InEditor";
import CUploadFile from "@/components/CUploadFile";
import { mapGetters, mapActions } from "vuex";
import VanFieldCheckbox from "@/components/VanField/VanFieldCheckbox";

export default {
  components: {
    InEditor,
    CUploadFile,
    VanFieldCheckbox
  },
  props: {
    parentObj: Object,
    parentparentObj: Object,
    closeName: {
      type: String,
      default: "关闭"
    }
  },
  data() {
    return {
      visible: false,
      loading: false,
      formFields: {
        UserIds: "",
        Users: [],
        Title: "",
        Text: "",
        Appendix: ""
      },
      entity: {},
      useroption: [],
      option: {
        label: "text",
        value: "value"
      },
      disabled: false
    };
  },
  mounted() {
    this.GetAllUser().then(res => {
      this.useroption = res;
    });
  },
  computed: {
    ...mapGetters(["userInfo"])
  },
  methods: {
    ...mapActions(["GetAllUser"]),
    openForm(id) {
      this.init();
      this.visible = true;
      this.loading = true;
      if (id) {
        this.$nextTick(() => {
          D_UserMail_GetTheData({ id: id }).then(resJson => {
            this.entity = resJson.Data;
            Object.keys(this.formFields).forEach(item => {
              this.formFields[item] = this.entity[item];
            });
            this.formFields.Users = this.formFields.UserIds.slice(1, -1).split(
              "^"
            );
            this.disabled = this.entity.Status !== 0;
            this.loading = false;
          });
        });
      } else {
        this.loading = false;
      }
    },
    init() {
      this.entity = {};
      this.formFields = {
        UserIds: "",
        Users: [],
        Title: "",
        Text: "",
        Appendix: ""
      };
      this.disabled = false;
    },
    handleSubmit(isDraft) {
      if (this.formFields.Users.length === 0) {
        this.$toast("收件人为空");
        return;
      }
      if (this.formFields.Title === "") {
        this.$toast("主题为空");
        return;
      }
      if (this.formFields.Text === "") {
        this.$toast("正文为空");
        return;
      }
      this.entity = Object.assign(this.entity, this.formFields);
      let userNames = [];
      this.formFields.Users.forEach(item => {
        let tempuser = this.useroption.find(d => d.value === item);
        if (tempuser !== null && typeof tempuser !== "undefined") {
          userNames.push(tempuser.text);
        } else {
          this.$toast("存在未知的收件人:" + item);
          return true;
        }
      });
      this.entity.UserNames = "^" + userNames.join("^") + "^";
      this.entity.UserIds = "^" + this.entity.Users.join("^") + "^";
      this.entity.Avatar = this.userInfo.Avatar;
      this.entity.Status = isDraft ? 0 : 1;

      this.loading = true;
      D_UserMail_SaveData(this.entity).then(resJson => {
        this.loading = false;

        if (resJson.Success) {
          this.$toast("操作成功");
          if (this.parentObj) {
            this.parentObj.getDataList();
          }
          this.init();
          this.visible = false;
        } else {
          this.$toast(resJson.Msg);
        }
      });
    },
    handleCancel() {
      this.visible = false;
      this.init();
      if (
        this.entity.Id > 0 &&
        (this.entity.ReadingMarks || "").indexOf(this.userInfo.Id) >= 0
      ) {
        if (this.parentObj) {
          this.parentObj.setDataRead(this.entity.Id);
        }
      }
    }
  }
};
</script>
